﻿using MediatR;
using T_Rbac_APi.Application.Command.DepartmentCommand;
using T_Rbac_Rep.Interface;

namespace T_Rbac_APi.Application.CommandHandler.DepartmentHandler
{
    public class DepartmentDeleteHandler : IRequestHandler<DepartmentDelteCommand, int>
    {
        private readonly IDapartment dapartment;
        private readonly IRuleInformation ruleInformation;

        public DepartmentDeleteHandler(IDapartment dapartment, IRuleInformation ruleInformation)
        {
            this.dapartment = dapartment;
            this.ruleInformation = ruleInformation;
        }

        public async Task<int> Handle(DepartmentDelteCommand request, CancellationToken cancellationToken)
        {
            var list = await dapartment.GetAloneByid(request.id);
            var results = await ruleInformation.GetvaluesByResult(x => x.Rdname==list.Dname);
            if (results == null)
            {
                return await Task.FromResult(-2);
            }
            list.IsDel=true;
            list.UpdateDate = DateTime.Now;
            list.UpdateBy = "小红";
            return await dapartment.Update(list);
        }
    }
}
